package com.googlecode.maven.plugin.appassembler;

import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateException;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.Writer;
import org.apache.commons.io.IOUtils;
import org.apache.maven.plugin.MojoFailureException;
import org.springframework.core.io.DefaultResourceLoader;
import org.springframework.core.io.Resource;
import org.springframework.core.io.ResourceLoader;

/**
 * Resource utils.
 */
public final class ResourceUtils
    {
    /**
     * FreeMarker configuration.
     */
    private static final Configuration CONFIGURATION = new Configuration();
    /**
     * Spring resource loader.
     */
    private static final ResourceLoader RESOURCE_LOADER = new DefaultResourceLoader();

    static
        {
        CONFIGURATION.setTemplateLoader(new ResourceTemplateLoader());
        }

    /**
     * Creates a new ResourceUtils object.
     */
    private ResourceUtils()
        {
        super();
        }

    /**
     * Returns the specified resource.
     *
     * @param resourceName Resource names.
     * @return The specified resource.
     */
    public static Resource getResource(String resourceName)
        {
        return RESOURCE_LOADER.getResource(resourceName);
        }

    /**
     * Copies a resource.
     *
     * @param resource The resource to copy.
     * @param targetFile The destination file.
     * @throws MojoFailureException If an I/O exception occurred.
     */
    public static void copyResource(Resource resource, File targetFile)
            throws MojoFailureException
        {
        InputStream istrm = null;
        try
            {
            istrm = resource.getInputStream();
            OutputStream ostrm = null;
            try
                {
                targetFile.getParentFile().mkdirs();
                ostrm = new FileOutputStream(targetFile);
                IOUtils.copy(istrm, ostrm);
                }
            catch (IOException e)
                {
                throw new MojoFailureException("Error writing resource file", e);
                }
            finally
                {
                IOUtils.closeQuietly(ostrm);
                }
            }
        catch (IOException e)
            {
            throw new MojoFailureException("Error reading resource file", e);
            }
        finally
            {
            IOUtils.closeQuietly(istrm);
            }
        }

    /**
     * Copies a template resource.
     *
     * @param templateName The template resource to copy.
     * @param targetFile The destination file.
     * @param vars Variables to be substituted in the resource template.
     * @throws MojoFailureException If an I/O or template exception occurred.
     */
    public static void copyTemplateResource(String templateName, File targetFile,
            Object vars)
            throws MojoFailureException
        {
        try
            {
            Template template = CONFIGURATION.getTemplate(templateName);
            OutputStream ostrm = null;
            try
                {
                targetFile.getParentFile().mkdirs();
                ostrm = new FileOutputStream(targetFile);
                Writer writer = null;
                try
                    {
                    writer = new OutputStreamWriter(ostrm, "UTF-8");
                    template.process(vars, writer);
                    }
                catch (TemplateException e)
                    {
                    throw new MojoFailureException(
                            "Error writing template resource file", e);
                    }
                finally
                    {
                    IOUtils.closeQuietly(writer);
                    }
                }
            catch (IOException e)
                {
                throw new MojoFailureException(
                        "Error writing template resource file", e);
                }
            finally
                {
                IOUtils.closeQuietly(ostrm);
                }
            }
        catch (IOException e)
            {
            throw new MojoFailureException(
                    "Could not read template resource file", e);
            }
        }
    }
